package com.chb.javabasic.Controller;

import com.chb.javabasic.BO.Student;

import java.io.*;

public class SerializableController {
    public static void serializable() throws IOException {
        Student student = new Student();
        student.setName("chen");
        student.setAge(20);

        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream( new File("student.txt") ) );
        oos.writeObject(student);
        oos.close();
        System.out.println("序列化成功！已经生成student.txt文件");
        System.out.println("==============================================");
    }

    public static void deserializable() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream =
                new ObjectInputStream( new FileInputStream( new File("student.txt") ) );
        Student student = (Student) objectInputStream.readObject();
        objectInputStream.close();

        System.out.println("反序列化结果为：");
        System.out.println( student );
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException {
       serializable();
       deserializable();
    }
}
